/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 悬浮播放器功能测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

import '../../lib/services/floating_ui_manager.dart';
import '../../lib/services/floating_player_service.dart';
import '../../lib/viewmodels/music_player_view_model.dart';
import '../../lib/models/music_item_model.dart';

void main() {
  group('悬浮播放器测试', () {
    late MusicPlayerViewModel musicPlayerViewModel;

    setUp(() {
      musicPlayerViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      musicPlayerViewModel.dispose();
      FloatingPlayerManager.dispose();
    });

    testWidgets('悬浮播放器初始化测试', (WidgetTester tester) async {
      await tester.pumpWidget(
        ChangeNotifierProvider.value(
          value: musicPlayerViewModel,
          child: MaterialApp(
            home: FloatingPlayerInitializer(
              child: Scaffold(
                body: Builder(
                  builder: (context) {
                    return ElevatedButton(
                      onPressed: () {
                        FloatingPlayerService.initialize(context);
                      },
                      child: const Text('初始化'),
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      );

      // 点击初始化按钮
      await tester.tap(find.text('初始化'));
      await tester.pumpAndSettle();

      // 验证悬浮播放器管理器已初始化
      expect(FloatingPlayerManager.isShowing, false);
    });

    testWidgets('悬浮播放器显示/隐藏测试', (WidgetTester tester) async {
      await tester.pumpWidget(
        ChangeNotifierProvider.value(
          value: musicPlayerViewModel,
          child: MaterialApp(
            home: FloatingPlayerInitializer(
              child: Scaffold(
                body: Builder(
                  builder: (context) {
                    return Column(
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            FloatingPlayerService.initialize(context);
                          },
                          child: const Text('初始化'),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            // 设置测试音乐
                            final testMusic = MusicItemModel(
                              id: 'test_001',
                              title: '测试歌曲',
                              artist: '测试艺术家',
                              album: '测试专辑',
                              audioUrl: 'test_url',
                            );
                            musicPlayerViewModel.setPlaylist([testMusic]);
                            FloatingPlayerService.showFloatingPlayer(context);
                          },
                          child: const Text('显示悬浮播放器'),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            FloatingPlayerService.hideFloatingPlayer();
                          },
                          child: const Text('隐藏悬浮播放器'),
                        ),
                      ],
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      );

      // 初始化
      await tester.tap(find.text('初始化'));
      await tester.pumpAndSettle();

      // 显示悬浮播放器
      await tester.tap(find.text('显示悬浮播放器'));
      await tester.pumpAndSettle();

      // 验证悬浮播放器已显示
      expect(FloatingPlayerManager.isShowing, true);

      // 隐藏悬浮播放器
      await tester.tap(find.text('隐藏悬浮播放器'));
      await tester.pumpAndSettle();

      // 验证悬浮播放器已隐藏
      expect(FloatingPlayerManager.isShowing, false);
    });

    test('悬浮播放器服务状态管理测试', () {
      // 测试页面状态管理
      expect(FloatingPlayerService.isShowing, false);

      // 测试进入播放页面
      FloatingPlayerService.setCurrentRoute('/music_player');

      // 测试离开播放页面
      FloatingPlayerService.setCurrentRoute('/other_page');
    });
  });
}
